﻿namespace FimCmTools.Common
{
    using System;
    using System.Configuration;
    using System.Security.Cryptography;
    using System.Text;

    /// <summary>
    /// 
    /// </summary>
    internal class Utilities
    {
        private const string CONFIGPREFIX = "FimCmTools";

        /// <summary>
        /// Encrypts the specified plain data string.
        /// </summary>
        /// <param name="data">The data.</param>
        /// <returns></returns>
        internal static byte[] Encrypt(string data)
        {
            if (string.IsNullOrEmpty(data))
                throw new ArgumentNullException("data", "data may not be null or empty");
            return Encrypt(Encoding.Unicode.GetBytes(data));
        }
        /// <summary>
        /// Encrypts the specified plain data.
        /// </summary>
        /// <param name="plainData">The plain data.</param>
        /// <returns></returns>
        internal static byte[] Encrypt(byte[] plainData)
        {
            if (null == plainData || plainData.Length == 0) throw new ArgumentNullException("plainData", "data may not be null or empty");
            return ProtectedData.Protect(plainData, null, DataProtectionScope.LocalMachine);
        }
        /// <summary>
        /// Decrypts the specified encrypted data.
        /// </summary>
        /// <param name="encryptedData">The encrypted data.</param>
        /// <returns></returns>
        internal static byte[] Decrypt(byte[] encryptedData)
        {
            if (null == encryptedData || encryptedData.Length == 0) throw new ArgumentNullException("encryptedData", "data may not be null or empty");
            return ProtectedData.Unprotect(encryptedData, null, DataProtectionScope.LocalMachine);
        }
       
        /// <summary>
        /// Gets a configured string.
        /// </summary>
        /// <param name="key">The key. it will be prefixed with FimCmTools.</param>
        /// <param name="defaultValue">The default value to return if the key is not present</param>
        /// <returns></returns>
        internal static string GetConfigString(string key, string defaultValue)
        {
            string realKey = CONFIGPREFIX+"."+key;
            string val = ConfigurationManager.AppSettings[realKey];
            if (!string.IsNullOrEmpty(val))
                return val;
            else
                return defaultValue;
        }

        /// <summary>
        /// Gets a  configured int32.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="defaultValue">The default value.</param>
        /// <returns></returns>
        internal static int GetConfigInt32(string key, int defaultValue)
        {
            string realKey = CONFIGPREFIX + "." + key;
            string val = ConfigurationManager.AppSettings[realKey];
            if (!string.IsNullOrEmpty(val))
            {
                int result = defaultValue;
                if (!System.Int32.TryParse(val, out result))
                    return defaultValue;
                return result;
            }
            else
                return defaultValue;
        }
    }
}
